package br.com.krk;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.widget.EditText;
import android.widget.SimpleAdapter;
import br.com.carcara.krk.R;
import br.com.control.Banco;

public class ListaProdutoActivity extends ListActivity {
	Banco _bd = new Banco(this);
	
	ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
	private SimpleAdapter sa;
	private EditText edtFiltro;
	
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lista_cliente);
	}
	@Override
	protected void onResume() {
	    	ListarProdutos(false);
	    	super.onResume();
	    }
	
	
	public void ListarProdutos(boolean usaFiltro){
		 HashMap<String,String> item;
	        _bd.open();
	        try{
	        	list.clear();
	        	Cursor _cursor = null;
	        	if (!usaFiltro){
	        		_cursor = _bd.consulta("Pro", new String[] { "*" },null,null,null,null,null,null);
	        	}else{
	        		_cursor = _bd.consulta("Pro", new String[] { "*" },null,null,null,null,null,null);
	        	}
	        	item = new HashMap<String,String>();
	        	_cursor.moveToFirst(); 
	        	if (_cursor.getCount() > 0){
		        	do{	
		        	  item = new HashMap<String,String>();
		        	  item.put( "line1", _cursor.getString(_cursor.getColumnIndex("cod_ret")).toString()+"-"+
		        			  			 _cursor.getString(_cursor.getColumnIndex("descricao")).toString()+" ,unid : "+
		        			  			_cursor.getString(_cursor.getColumnIndex("unid")).toString());
		        	  item.put( "line2", "Saldo : "+_cursor.getString(_cursor.getColumnIndex("saldo")).toString());
			          list.add( item );
			        }while (_cursor.moveToNext());	
	        	}
	        	
	        
	        	_cursor.close();
	        	_bd.fechaBanco();
	        	
	        	
	        }catch(Exception e){
	                     
	        } 
	        sa = new SimpleAdapter(this, list,R.layout.lista_padrao, new String[] { "line1","line2" }, new int[] {R.id.line_a, R.id.line_b});
		    setListAdapter(sa);
	 
	
	 }
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		
		getMenuInflater().inflate(R.menu.lista_produto, menu);
		return true;
	}

}
